# Contributor: David Huffman <storedbox@outlook.com>
# Maintainer: Leon Marz <main@lmarz.org>
pkgname=onetbb
pkgver=2021.12.0
pkgrel=0
pkgdesc="oneAPI Threading Building Blocks"
url="https://github.com/oneapi-src/oneTBB"
arch="all"
license="GPL-2.0-only"
depends_dev="linux-headers"
makedepends="
	$depends_dev
	cmake
	hwloc-dev
	libucontext-dev
	py3-setuptools
	python3-dev
	samurai
	swig
	"
subpackages="$pkgname-dbg py3-tbb-pyc py3-tbb:_py3 $pkgname-dev $pkgname-doc"
source="tbb-$pkgver.tar.gz::https://github.com/oneapi-src/oneTBB/archive/v$pkgver.tar.gz
	enable-resumable.patch
	tbb.pc
	tbbmalloc.pc
	tbbmalloc_proxy.pc
	"
builddir="$srcdir/oneTBB-$pkgver"
# a dozen+ tests hang forever with no timeout
options="!check"

provides="libtbb=$pkgver-r$pkgrel"
replaces="libtbb"

prepare() {
	default_prepare

	sed -e "s@#define MALLOC_UNIXLIKE_OVERLOAD_ENABLED __linux__@@"  \
		-i src/tbbmalloc_proxy/proxy.h

	sed -i "s|@@VERSION@@|$pkgver|" -i "$srcdir"/*.pc
}

build() {
	CFLAGS="$CFLAGS -O2 -DNDEBUG -flto=auto" \
	CXXFLAGS="$CXXFLAGS -U_FORTIFY_SOURCE -O2 -DNDEBUG -flto=auto" \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DTBB4PY_BUILD=ON \
		-DTBB_STRICT=OFF \
		-DTBB_TEST="$(want_check && echo ON || echo OFF)"
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	install -Dm0644 "$srcdir"/*.pc -t "$pkgdir"/usr/lib/pkgconfig
}

dev() {
	default_dev
	provides="libtbb-dev=$pkgver-r$pkgrel"
	replaces="libtbb-dev"
}

doc() {
	default_doc
	provides="libtbb-doc=$pkgver-r$pkgrel"
	replaces="libtbb-doc"
}

_py3() {
	pkgdesc="$pkgdesc (python bindings)"
	amove usr/lib/python3.*
}

sha512sums="
64022bcb61cf7b2030a1bcc11168445ef9f0d69b70290233a7febb71cc7a12cc2282dddc045f84e30893efe276342f02fd78d176706268eeaefe9aac7446d4e9  tbb-2021.12.0.tar.gz
585efb64717deb5cb12c8dfb6c65214266e3f8cf1cdaf1032ee8e497c9303810c4137fb1575a926f11f14663c8bb907789726aaa668340e96de0dedb7393018d  enable-resumable.patch
b7849e84c5f4d868ad19dc4a2433492facd1f75fe6e5cb0afc334e8f07bb4657d318ee1bc53f7890bfb8a87c7233bba85b61aba2aec9ea3dc24652a4bdec4dde  tbb.pc
25b0bfe2067f287064f81dc434ac70336ad42fdd7fa91cded2e467f3939000480e1012675dc4e435a1712cf9b968cb565f213dfffb3f69ad46929aec237b9a02  tbbmalloc.pc
5952862755736fb85982281b3953183ed9d4657497a7f6bae1ef66bd1e2265882773764f07e46679b2aa750f80f47cc2a019024822ab5fa272b718c539ef3462  tbbmalloc_proxy.pc
"
